<!--
 * @Author: zw 775925302@qq.com
 * @Date: 2023-03-19 21:41:23
 * @LastEditors: zw 775925302@qq.com
 * @LastEditTime: 2023-03-20 07:05:45
-->

<template>
    <zw-modal v-model="visible" title="导入表" width="60%" height="80%">
        <ZwTemplateListPage :schema="schema" ref="refListPage">

        </ZwTemplateListPage>

        <template #footer>
            <el-button type="primary" @click="onConfirm">确 定</el-button>
        </template>
    </zw-modal>
</template>

<script setup lang="ts">

import { ref } from "vue"
import { schema } from './schema'
import { VXETable } from 'vxe-table'
import request from '@/config/axios'

const refListPage = ref()
const emit = defineEmits(['ok'])
const visible = ref(false)

const onConfirm = async () => {
    let gridObj = refListPage.value.refGird();
    if (gridObj.getCheckboxRecords().length === 0) {
        VXETable.modal.message({ content: '请选择要导入的表', status: 'warning' })
        return
    }

    /** 多选框选中数据 */
    const tables = ref<string[]>([])
    gridObj.getCheckboxRecords().forEach((item) => {
        tables.value.push(item.name)
    })
    const reqBody = {
        method: "post",
        url: "/infra/codegen/create-list",
        params: {
            dataSourceConfigId: 0,
            tableNames: tables.value
        }
    }
    let [data, err] = await request.request(reqBody)
    if (data) {
        emit('ok')
        handleClose()
    } else {
        VXETable.modal.message({ content: '保存错误:' + err.msg, status: 'error' })

    }
}

const show = ()=>{
    visible.value = true;
}

const handleClose = () => {
  visible.value = false
}

defineExpose({
  show
})

</script>